<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-21 16:00:39
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-05-25 17:44:21
 */
namespace app\weixin\model;
use app\BaseModel;
use think\facade\Cache;
use think\Db;
//*------------------------------------------------------ */
//-- 视频号商品类目模型
/*------------------------------------------------------ */
class WeixinVideoCateModel extends BaseModel
{
	protected $table = 'weixin_video_cate';
	public  $pk = 'id';
	protected $mkey = 'weixin_video_cate_';
	

    /*------------------------------------------------------ */
    //-- 清除缓存
    /*------------------------------------------------------ */
    public function cleanMemcache()
    {
        Cache::rm($this->mkey);
    }

	/**
	 * [getShopApply 自定义版交易组件类目申请]
	 * @param  array  $param [请求参数]
	 * @return array         [请求返回]
	 */
    function auditCategory($param = array()){     

        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/audit/audit_category?access_token=$ACCESS_TOKEN";
        return $this->curlCategory($url,$param);
    }
    /**
     *  [getRows 获取类目列表]
     * @param  array  $where [搜索条件]
     * @return [type]        [description]
     */
    public function getRows($where = [])
    {
        // $rows = Cache::get($this->mkey);
        // if (empty($rows)) {
            $rows = $this->where($where)->select()->toArray();
            Cache::set($this->mkey,$rows);
        // }
        return $rows;
    }

    /**
     * [info 分类详情]
     */
    public function info($id)
    {
        $info = $this->find($id);
        if (empty($info)) {
            return '类目不存在';
        }
        $info = $info->toArray();
        if ($info['status'] == 0) {
            $data = [];
            $data['audit_id'] = $info['audit_id'];
            $res = $this->auditResult($data);
            if ($res['errcode'] > 0 && $is_post) {
                return $this->error($res['errmsg']);
            }
            $upData = [];
            if ($res['data']['status'] != $info['status']) {
                $upData['status'] = $res['data']['status'];
                $upData['update_time'] = time();
                $upData['reject_reason'] = $res['data']['reject_reason'];
                $ress = $this->Model->where('id',$info['id'])->update($upData);
                if ($ress < 1) {
                    return '修改状态失败';
                }
            }
            $info['status'] = $res['data']['status'];
            $info['reject_reason'] = $res['data']['reject_reason'];

        }
        
        if (empty($info['certificate']) == false) {
            $info['certificate'] = explode(',',$info['certificate']);
        }
        return $info;
    }


    /**
	 * [getShopApply 自定义版交易组件查询审核结果]
	 * @param  array  $param [请求参数]
	 * @return array         [请求返回]
	 */
    function auditResult($param = array()){     

        $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
        $url = "https://api.weixin.qq.com/shop/audit/result?access_token=$ACCESS_TOKEN";
        return $this->curlCategory($url,$param);
    }


    public function getCategoryList($is_Cache = true)
    {
        $count = Cache::get($this->mkey.'count_');
        if (empty($count) || $this->mkey == false) {

            $shopCat = $this->getShopCat();

            $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
            $url = "https://api.weixin.qq.com/shop/account/get_category_list?access_token=$ACCESS_TOKEN";
            $res = $this->curlCategory($url);

            foreach ($res['data'] as $key => $value) {
                $info = $this->where('third_cat_id',$value['third_cat_id'])->find();
                if (empty($info)) {
                    $catInfo = $shopCat['third_cat_id'];
                    if (empty($catInfo) == false) {
                        $catInfo['add_time'] = time();
                        $ress = $this::create($catInfo);
                        if (empty($ress)) {
                            return '同步类目信息失败.';
                        }
                    }
                }
            }

            $count = count($res['data']);
            Cache::set($this->mkey.'count_',$count);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 自定义版交易组件及开放接口 获取所有三级类目及其资质相关信息
    /*------------------------------------------------------ */
    function getShopCat($is_cache = true){     
        $cate = Cache::get('xcx_shop_cat');
        if (empty($cate) || $is_cache == false) {
            $ACCESS_TOKEN = (new WeiXinMpModel)->getAccessToken();
            $url = "https://api.weixin.qq.com/shop/cat/get?access_token=$ACCESS_TOKEN";
            $result = $this->curlCategory($url);           

            $cate = [];
            foreach ($result['third_cat_list'] as $key => $value) {
                $cate[$value['third_cat_id']] = $value;
            }
            Cache::set('xcx_shop_cat',$cate,86400);   
        }
        
        return $cate;
    }

    /**
     * [curlCategory curl]
     * @param  string $url   [description]
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function curlCategory($url='',$param = array())
    {
    	$data = json_encode($param);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return json_decode($result,true);
    }
}